package com.fs.cv.controller;

import com.fs.cv.pojo.pojodao.User;
import com.fs.cv.pojo.pojoview.StudentCourse;
import com.fs.cv.service.StudentCourseService;
import com.fs.cv.service.impl.StudentCourseServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>教师管理学生成绩控制层</p>
 *
 * @author : 苏明友
 * @date : 2020-09-29 14:38
 **/
@Controller
@RequestMapping("/studentCourse")
public class StudentCourseController {
    @Autowired  //自动装配
            StudentCourseService studentCourseService;

    /**
     * 方法描述:老师查看并且管理所有学生的成绩，并将其显示在成绩管理的前端页面上
     *
     * @参数 []
     * @返回值 java.util.Map<java.lang.String, java.lang.Object>
     * @作者 苏明友
     * @时间 2020/9/29
     */
    @RequestMapping("/getStudentCourse")
    public String getAllStudentCourseByStudentId(Model model) {
        //获取存储的session
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        int userId = user.getUserId();
        System.out.println("user = " + user);
        List<StudentCourse> studentCourses = studentCourseService.getAllStudentCourseByStudentId(userId);
        model.addAttribute("studentCourses", studentCourses);
        return "score_data";
    }

    /**
     * 方法描述:使用学生姓名来进行模糊查询学生的成绩进行管理
     *
     * @参数 [studentNameValue]
     * @返回值 java.util.Map<java.lang.String, java.lang.Object>
     * @作者 苏明友
     * @时间 2020/9/29
     */
    @RequestMapping("/getStudentByName")
    @ResponseBody
    public Map<String, Object> getStudentByName(String studentNameValue) {
        System.out.println(studentNameValue);
        //获取存储的session
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        int userId = user.getUserId();
        List<StudentCourse> studentList = studentCourseService.getStudentByName(studentNameValue, userId);
        System.out.println("studentList = " + studentList);
        HashMap<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("message", "获取用户数据成功");
        result.put("studentList", studentList);
        return result;
    }

    /**
     * 方法描述:根据学生的id逻辑删除学生的成绩信息
     *
     * @参数 [studentId]
     * @返回值 java.util.Map<java.lang.String, java.lang.Object>
     * @作者 苏明友
     * @时间 2020/10/8
     */
    @RequestMapping("/deleteStudentScoreByStudentId")
    @ResponseBody
    public Map<String, Object> deleteStudentScoreByStudentId(Integer studentId) {
        Map<String, Object> result = new HashMap<>();
        int deleted = studentCourseService.deleteStudentScoreByStudentId(studentId);
        System.out.println("deleted = " + deleted);
        if (deleted > 0) {
            result.put("code", 0);
            result.put("message", "删除用户数据成功");
        } else {
            result.put("code", -1);
            result.put("message", "删除用户数据失败");
        }
        return result;
    }

    /**
     * 方法描述:根据学生的id查询学生的课程以及所对应的成绩信息，然后显示到成绩编辑的模态框中
     *
     * @参数 [studentId]
     * @返回值 java.util.Map<java.lang.String, java.lang.Object>
     * @作者 苏明友
     * @时间 2020/10/8
     */
    @RequestMapping("/getCourseByStudentId")
    @ResponseBody
    public Map<String, Object> getCourseByStudentId(Integer studentId) {
        System.out.println(studentId);
        Map<String, Object> result = new HashMap<>();
        StudentCourse studentCourse = studentCourseService.getCourseByStudentId(studentId);
        int courseId = studentCourse.getCourseId();
        int scoreId = studentCourse.getScoreId();
        System.out.println("scoreId = " + scoreId);
        System.out.println("courseId = " + courseId);
        System.out.println("studentCourse = " + studentCourse);
        //判断查询到的用户数据是否存在
        if (studentCourse == null) {
            result.put("code", -1);
            result.put("message", "获取用户数据失败");
        } else {
            result.put("code", 0);
            result.put("message", "获取用户数据成功");
            result.put("studentCourse", studentCourse);
        }
        return result;
    }

    /**
     * 方法描述:编辑学生的课程以及对应的成绩
     *
     * @参数 [studentCourse]
     * @返回值 java.util.Map<java.lang.String, java.lang.Object>
     * @作者 苏明友
     * @时间 2020/10/9
     */
    @RequestMapping("/editScoreById")
    @ResponseBody
    @Transactional
    public Map<String, Object> editScoreById(StudentCourse studentCourse) {
        System.out.println("studentCourse = " + studentCourse.getCourseId());
        Map<String, Object> result = new HashMap<>();
        int num = studentCourseService.editScoreById(studentCourse);
        int i = studentCourseService.editCourseById(studentCourse);
        int x = num + i;
        if (x >= 2) {
            result.put("code", 0);
            result.put("message", "学生成绩修改成功");
        }
        return result;
    }
}
